// --- 病例状态常量映射 ---
const CASE_STATUS_MAP = {
    FINISH: { name: '已完成', class: 'finish' },
    DRAFT: { name: '草稿', class: 'draft' },
    PENDING: { name: '待审核', class: 'pending' }, // 示例状态
    NEED_FOLLOWUP: { name: '需复诊', class: 'pending' },
  };
  
  /**
   * 封装病例记录列表的 Mock 数据
   */
  const getCaseListData = () => {
    // 模拟数据结构：以单次就诊记录为单位
    const rawData = [
      { recordId: 'REC0015', patientId: 'P00123', patientName: '李思涵', date: '2025-11-20', project: '隐适美复诊 (15/40)', summary: '更换牙套，附件检查...', status: 'FINISH' },
      { recordId: 'REC0016', patientId: 'P00456', patientName: '张建国', date: '2025-11-25', project: '初诊检查', summary: '全景片拍摄，口扫...', status: 'PENDING' },
      { recordId: 'REC0017', patientId: 'P00123', patientName: '李思涵', date: '2025-11-26', project: '种植方案设计（草稿）', summary: '正在制定，数据未锁定。', status: 'DRAFT' },
      { recordId: 'REC0018', patientId: 'P01334', patientName: '孙明', date: '2025-10-15', project: '拔牙手术', summary: '拔除左上智齿，恢复良好。', status: 'FINISH' },
      { recordId: 'REC0019', patientId: 'P00789', patientName: '王梓涵', date: '2025-10-01', project: '正畸初诊', summary: '初步正畸评估，方案待定。', status: 'NEED_FOLLOWUP' },
    ];
    
    return rawData.map(p => ({
      ...p,
      statusName: CASE_STATUS_MAP[p.status].name,
      statusClass: CASE_STATUS_MAP[p.status].class,
    }));
  };
  
  Page({
    data: {
      allCaseList: [], 
      filteredList: [],    
      currentStatus: 'ALL', 
      searchKeyword: '',   
      statusTabs: [        
        { key: 'ALL', name: '全部记录', count: 0 },
        { key: 'FINISH', name: '已完成' },
        { key: 'PENDING', name: '待处理' },
        { key: 'DRAFT', name: '草稿' },
      ],
    },
  
    onLoad() {
      this.fetchCaseList();
    },
  
    /**
     * 加载/刷新病例列表数据
     */
    fetchCaseList() {
      const list = getCaseListData();
      
      const counts = list.reduce((acc, p) => { acc[p.status] = (acc[p.status] || 0) + 1; return acc; }, {});
      
      const updatedTabs = this.data.statusTabs.map(tab => {
          tab.count = tab.key === 'ALL' ? list.length : counts[tab.key] || 0;
          return tab;
      });
  
      this.setData({
        allCaseList: list,
        statusTabs: updatedTabs
      }, this.filterAndSearch);
    },
  
    /**
     * 模糊搜索输入：搜索患者名、内容或编号
     */
    onSearchInput(e) {
      this.setData({ searchKeyword: e.detail.value }, this.filterAndSearch);
    },
  
    /**
     * 切换状态 Tab (分类)
     */
    switchStatusTab(e) {
      this.setData({ currentStatus: e.currentTarget.dataset.key }, this.filterAndSearch);
    },
  
    /**
     * 核心筛选和搜索逻辑
     */
    filterAndSearch() {
      const keyword = this.data.searchKeyword.toLowerCase();
      const status = this.data.currentStatus;
      
      let result = this.data.allCaseList;
  
      // 1. 按状态筛选 (分类)
      if (status !== 'ALL') {
          result = result.filter(p => p.status === status);
      }
  
      // 2. 模糊搜索
      if (keyword) {
          result = result.filter(p => 
              p.patientName.toLowerCase().includes(keyword) || 
              p.project.toLowerCase().includes(keyword) || 
              p.summary.toLowerCase().includes(keyword) ||
              p.recordId.toLowerCase().includes(keyword) 
          );
      }
  
      this.setData({ filteredList: result });
    },
  
    /**
     * 查看病例详情 (点击后才是详情页)
     */
    viewCaseDetail(e) {
      const { patientId, recordId } = e.currentTarget.dataset;
      // 跳转到单个病例记录的详情页 (原 case-management)
      wx.navigateTo({
        url: `/pages/doctor/case-management/case-management?patientId=${patientId}&recordId=${recordId}`
      });
    },
  
    /**
     * 新建诊疗记录
     */
    createNewRecord() {
      wx.navigateTo({
        url: '/pages/treatment-record/treatment-record' // 可带参数进入新建模式
      });
    },
  
    /**
     * 排序/高级筛选 (模拟弹出)
     */
    showSortOptions() {
      wx.showActionSheet({
        itemList: ['按就诊日期排序 (新->旧)', '按就诊项目类型筛选', '按主治医生筛选'],
        success: () => {
          wx.showToast({ title: '已应用排序/筛选', icon: 'none' });
        }
      });
    }
  });